package com.esri.core.geometry;

import com.github.mikephil.charting.utils.Utils;
import java.nio.ByteBuffer;
import java.util.Arrays;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public final class AttributeStreamOfInt32 extends AttributeStreamBase {
    int[] a;
    int b;

    /* loaded from: classes.dex */
    public static abstract class IntComparator {
        public abstract int compare(int i, int i2);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public static class RandomSeed {
        public int random = 1973;
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public int getPersistence() {
        return 2;
    }

    public void reserve(int i) {
        if (i <= 0) {
            return;
        }
        int[] iArr = this.a;
        if (iArr == null) {
            this.a = new int[i];
        } else if (i <= iArr.length) {
        } else {
            int[] iArr2 = new int[i];
            System.arraycopy(iArr, 0, iArr2, 0, iArr.length);
            this.a = iArr2;
        }
    }

    public int size() {
        return this.b;
    }

    public AttributeStreamOfInt32(int i) {
        this.a = null;
        this.a = new int[i >= 2 ? i : 2];
        this.b = i;
    }

    public AttributeStreamOfInt32(int i, int i2) {
        this.a = null;
        int[] iArr = new int[i >= 2 ? i : 2];
        this.a = iArr;
        this.b = i;
        Arrays.fill(iArr, 0, i, i2);
    }

    public AttributeStreamOfInt32(AttributeStreamOfInt32 attributeStreamOfInt32) {
        this.a = null;
        this.a = (int[]) attributeStreamOfInt32.a.clone();
        this.b = attributeStreamOfInt32.b;
    }

    public AttributeStreamOfInt32(AttributeStreamOfInt32 attributeStreamOfInt32, int i) {
        this.a = null;
        int size = attributeStreamOfInt32.size();
        i = size <= i ? size : i;
        int[] iArr = new int[i >= 2 ? i : 2];
        this.a = iArr;
        System.arraycopy(attributeStreamOfInt32.a, 0, iArr, 0, i);
    }

    public int read(int i) {
        return this.a[i];
    }

    public int get(int i) {
        return this.a[i];
    }

    public void write(int i, int i2) {
        if (this.m_bReadonly) {
            throw new RuntimeException("invalid_call");
        }
        this.a[i] = i2;
    }

    public void set(int i, int i2) {
        if (this.m_bReadonly) {
            throw new RuntimeException("invalid_call");
        }
        this.a[i] = i2;
    }

    public void add(int i) {
        resize(this.b + 1);
        this.a[this.b - 1] = i;
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public AttributeStreamBase restrictedClone(int i) {
        int i2 = this.b;
        if (i >= i2) {
            i = i2;
        }
        int[] iArr = new int[i];
        System.arraycopy(this.a, 0, iArr, 0, i);
        this.a = iArr;
        this.b = i;
        return this;
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public int virtualSize() {
        return size();
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public double readAsDbl(int i) {
        return read(i);
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public int readAsInt(int i) {
        return read(i);
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public long readAsInt64(int i) {
        return read(i);
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public void resize(int i) {
        if (this.m_bLockedInSize) {
            throw new GeometryException("invalid call. Attribute Stream is locked and cannot be resized.");
        }
        if (i <= this.b) {
            int i2 = (i * 5) / 4;
            int[] iArr = this.a;
            if (i2 < iArr.length) {
                int[] iArr2 = new int[i];
                System.arraycopy(iArr, 0, iArr2, 0, i);
                this.a = iArr2;
            }
            this.b = i;
            return;
        }
        if (i > this.a.length) {
            int[] iArr3 = new int[i < 64 ? Math.max(i * 2, 4) : (i * 5) / 4];
            System.arraycopy(this.a, 0, iArr3, 0, this.b);
            this.a = iArr3;
        }
        this.b = i;
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public void resizePreserveCapacity(int i) {
        int[] iArr = this.a;
        if (iArr == null || i > iArr.length) {
            resize(i);
        }
        if (this.m_bLockedInSize) {
            throw new GeometryException("invalid call. Attribute Stream is locked and cannot be resized.");
        }
        this.b = i;
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public void resize(int i, double d) {
        if (this.m_bLockedInSize) {
            throw new GeometryException("invalid call. Attribute Stream is locked and cannot be resized.");
        }
        if (i <= this.b) {
            int i2 = (i * 5) / 4;
            int[] iArr = this.a;
            if (i2 < iArr.length) {
                int[] iArr2 = new int[i];
                System.arraycopy(iArr, 0, iArr2, 0, i);
                this.a = iArr2;
            }
            this.b = i;
            return;
        }
        if (i > this.a.length) {
            int[] iArr3 = new int[i < 64 ? Math.max(i * 2, 4) : (i * 5) / 4];
            System.arraycopy(this.a, 0, iArr3, 0, this.b);
            this.a = iArr3;
        }
        Arrays.fill(this.a, this.b, i, (int) d);
        this.b = i;
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public void writeAsDbl(int i, double d) {
        write(i, (int) d);
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public void writeAsInt64(int i, long j) {
        write(i, (int) j);
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public void writeAsInt(int i, int i2) {
        write(i, i2);
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public int calculateHashImpl(int i, int i2, int i3) {
        int size = size();
        while (i2 < size && i2 < i3) {
            i = NumberUtils.hash(i, read(i2));
            i2++;
        }
        return i;
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public boolean equals(AttributeStreamBase attributeStreamBase, int i, int i2) {
        if (attributeStreamBase != null && (attributeStreamBase instanceof AttributeStreamOfInt32)) {
            AttributeStreamOfInt32 attributeStreamOfInt32 = (AttributeStreamOfInt32) attributeStreamBase;
            int size = size();
            int size2 = attributeStreamOfInt32.size();
            if (i2 > size || (i2 > size2 && size != size2)) {
                return false;
            }
            if (i2 > size) {
                i2 = size;
            }
            while (i < i2) {
                if (read(i) != attributeStreamOfInt32.read(i)) {
                    return false;
                }
                i++;
            }
            return true;
        }
        return false;
    }

    public int getLast() {
        return this.a[this.b - 1];
    }

    public void removeLast() {
        resize(this.b - 1);
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public void addRange(AttributeStreamBase attributeStreamBase, int i, int i2, boolean z, int i3) {
        if (this.m_bReadonly) {
            throw new GeometryException("invalid_call");
        }
        if (!z && (i3 < 1 || i2 % i3 != 0)) {
            throw new IllegalArgumentException();
        }
        int i4 = this.b;
        resize(i4 + i2);
        if (z) {
            System.arraycopy(((AttributeStreamOfDbl) attributeStreamBase).a, i, this.a, i4, i2);
            return;
        }
        int i5 = i2;
        int i6 = 0;
        while (i6 < i2) {
            i5 -= i3;
            for (int i7 = 0; i7 < i3; i7++) {
                this.a[i4 + i6 + i7] = ((AttributeStreamOfInt32) attributeStreamBase).a[i + i5 + i7];
            }
            i6 += i3;
        }
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public void insertRange(int i, AttributeStreamBase attributeStreamBase, int i2, int i3, boolean z, int i4, int i5) {
        if (this.m_bReadonly) {
            throw new GeometryException("invalid_call");
        }
        if (!z && (i4 < 1 || i3 % i4 != 0)) {
            throw new IllegalArgumentException();
        }
        int[] iArr = this.a;
        System.arraycopy(iArr, i, iArr, i + i3, i5 - i);
        int[] iArr2 = this.a;
        AttributeStreamOfInt32 attributeStreamOfInt32 = (AttributeStreamOfInt32) attributeStreamBase;
        int[] iArr3 = attributeStreamOfInt32.a;
        if (iArr2 == iArr3 && i < i2) {
            i2 += i3;
        }
        if (z) {
            System.arraycopy(iArr3, i2, iArr2, i, i3);
            return;
        }
        int i6 = i3;
        int i7 = 0;
        while (i7 < i3) {
            i6 -= i4;
            for (int i8 = 0; i8 < i4; i8++) {
                this.a[i + i7 + i8] = attributeStreamOfInt32.a[i2 + i6 + i8];
            }
            i7 += i4;
        }
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public void insertRange(int i, double d, int i2, int i3) {
        if (this.m_bReadonly) {
            throw new GeometryException("invalid_call");
        }
        int[] iArr = this.a;
        int i4 = i2 + i;
        System.arraycopy(iArr, i, iArr, i4, i3 - i);
        Arrays.fill(this.a, i, i4, (int) d);
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public void insertAttributes(int i, Point point, int i2, int i3) {
        if (this.m_bReadonly) {
            throw new GeometryException("invalid_call");
        }
        int componentCount = VertexDescription.getComponentCount(i2);
        int[] iArr = this.a;
        System.arraycopy(iArr, i, iArr, i + componentCount, i3 - i);
        for (int i4 = 0; i4 < componentCount; i4++) {
            this.a[i + i4] = (int) point.a(i2, i4);
        }
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public void eraseRange(int i, int i2, int i3) {
        if (this.m_bReadonly) {
            throw new GeometryException("invalid_call");
        }
        int i4 = i + i2;
        if (i4 > this.b) {
            throw new GeometryException("invalid_call");
        }
        int[] iArr = this.a;
        System.arraycopy(iArr, i4, iArr, i, i3 - i4);
        this.b -= i2;
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public void readRange(int i, int i2, ByteBuffer byteBuffer, int i3, boolean z) {
        if (i < 0 || i2 < 0 || i3 < 0 || size() < i2 + i) {
            throw new IllegalArgumentException();
        }
        int sizeOf = NumberUtils.sizeOf((double) Utils.DOUBLE_EPSILON);
        if (byteBuffer.capacity() < (sizeOf * i2) + i3) {
            throw new IllegalArgumentException();
        }
        if (i2 == 0) {
            return;
        }
        if (!z) {
            i += i2 - 1;
        }
        int i4 = z ? 1 : -1;
        int i5 = 0;
        while (i5 < i2) {
            byteBuffer.putInt(i3, this.a[i]);
            i += i4;
            i5++;
            i3 += sizeOf;
        }
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public void reverseRange(int i, int i2, int i3) {
        if (this.m_bReadonly) {
            throw new GeometryException("invalid_call");
        }
        if (i3 < 1 || i2 % i3 != 0) {
            throw new GeometryException("invalid_call");
        }
        int i4 = i2 >> 1;
        int i5 = 0;
        while (i5 < i4) {
            i2 -= i3;
            for (int i6 = 0; i6 < i3; i6++) {
                int[] iArr = this.a;
                int i7 = i + i5 + i6;
                int i8 = iArr[i7];
                int i9 = i + i2 + i6;
                iArr[i7] = iArr[i9];
                iArr[i9] = i8;
            }
            i5 += i3;
        }
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public void setRange(double d, int i, int i2) {
        int i3;
        if (i < 0 || i2 < 0 || i < 0 || (i3 = i2 + i) > size()) {
            throw new IllegalArgumentException();
        }
        Arrays.fill(this.a, i, i3, (int) d);
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public void writeRange(int i, int i2, AttributeStreamBase attributeStreamBase, int i3, boolean z, int i4) {
        if (i < 0 || i2 < 0 || i3 < 0) {
            throw new IllegalArgumentException();
        }
        if (!z && (i4 <= 0 || i2 % i4 != 0)) {
            throw new IllegalArgumentException();
        }
        AttributeStreamOfInt32 attributeStreamOfInt32 = (AttributeStreamOfInt32) attributeStreamBase;
        int i5 = i3 + i2;
        if (attributeStreamOfInt32.size() < i5) {
            throw new IllegalArgumentException();
        }
        if (i2 == 0) {
            return;
        }
        int i6 = i2 + i;
        if (size() < i6) {
            resize(i6);
        }
        if (attributeStreamBase == this) {
            a(i, i2, i3, z, i4);
        } else if (z) {
            System.arraycopy(attributeStreamOfInt32.a, i3, this.a, i, i2);
        } else {
            int i7 = i5 - i4;
            if (i4 == 1) {
                for (int i8 = 0; i8 < i2; i8++) {
                    this.a[i] = attributeStreamOfInt32.a[i7];
                    i++;
                    i7--;
                }
                return;
            }
            int i9 = i2 / i4;
            for (int i10 = 0; i10 < i9; i10++) {
                for (int i11 = 0; i11 < i4; i11++) {
                    this.a[i + i11] = attributeStreamOfInt32.a[i7 + i11];
                }
                i += i4;
                i7 -= i4;
            }
        }
    }

    private void a(int i, int i2, int i3, boolean z, int i4) {
        if (z && i == i3) {
            return;
        }
        int[] iArr = this.a;
        System.arraycopy(iArr, i3, iArr, i, i2);
        if (z) {
            return;
        }
        int i5 = (i + i2) - i4;
        int i6 = i2 / 2;
        for (int i7 = 0; i7 < i6; i7++) {
            for (int i8 = 0; i8 < i4; i8++) {
                int[] iArr2 = this.a;
                int i9 = i + i8;
                int i10 = iArr2[i9];
                int i11 = i5 + i8;
                iArr2[i9] = iArr2[i11];
                iArr2[i11] = i10;
            }
            i += i4;
            i5 -= i4;
        }
    }

    @Override // com.esri.core.geometry.AttributeStreamBase
    public void writeRange(int i, int i2, ByteBuffer byteBuffer, int i3, boolean z) {
        if (i < 0 || i2 < 0 || i3 < 0) {
            throw new IllegalArgumentException();
        }
        int sizeOf = NumberUtils.sizeOf((double) Utils.DOUBLE_EPSILON);
        if (byteBuffer.capacity() < (sizeOf * i2) + i3) {
            throw new IllegalArgumentException();
        }
        if (i2 == 0) {
            return;
        }
        int i4 = i2 + i;
        if (size() < i4) {
            resize(i4);
        }
        if (!z) {
            i += i2 - 1;
        }
        int i5 = z ? 1 : -1;
        int i6 = 0;
        while (i6 < i2) {
            this.a[i] = byteBuffer.getInt(i3);
            i += i5;
            i6++;
            i3 += sizeOf;
        }
    }

    public void Sort(int i, int i2, IntComparator intComparator) {
        if (i2 - i < 10) {
            a(i, i2, intComparator);
        } else {
            a(i, i2 - 1, intComparator, new RandomSeed());
        }
    }

    void a(int i, int i2, IntComparator intComparator) {
        for (int i3 = i; i3 < i2; i3++) {
            int i4 = this.a[i3];
            int i5 = i3 - 1;
            while (i5 >= i && intComparator.compare(this.a[i5], i4) > 0) {
                int[] iArr = this.a;
                iArr[i5 + 1] = iArr[i5];
                i5--;
            }
            this.a[i5 + 1] = i4;
        }
    }

    void a(int i, int i2) {
        int[] iArr = this.a;
        int i3 = iArr[i2];
        iArr[i2] = iArr[i];
        iArr[i] = i3;
    }

    void a(int i, int i2, IntComparator intComparator, RandomSeed randomSeed) {
        if (i >= i2) {
            return;
        }
        while (true) {
            int i3 = i2 - i;
            if (i3 < 9) {
                a(i, i2 + 1, intComparator);
                return;
            }
            randomSeed.random = NumberUtils.nextRand(randomSeed.random);
            int intMax = ((int) ((i3 * randomSeed.random) / NumberUtils.intMax())) + i;
            int i4 = this.a[intMax];
            a(intMax, i2);
            int i5 = i;
            int i6 = i5;
            while (i5 < i2) {
                if (intComparator.compare(this.a[i5], i4) <= 0) {
                    a(i6, i5);
                    i6++;
                }
                i5++;
            }
            a(i6, i2);
            if (i6 - i < i2 - i6) {
                a(i, i6 - 1, intComparator, randomSeed);
                i = i6 + 1;
            } else {
                a(i6 + 1, i2, intComparator, randomSeed);
                i2 = i6 - 1;
            }
        }
    }
}
